<template>
  <el-dialog
    ref="refDlg"
    :title="detailData.id?'编辑':'新增'"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    append-to-body
    width="50%"
    @close="closeDialog"
  >
    <div>
      <el-form
        ref="refForm"
        :rules="formRules"
        :model="formData"
        size="mini"
        label-width="100px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="启用状态:" prop="enable">
              <el-radio-group v-model="formData.enable">
                <el-radio :label="1">启用</el-radio>
                <el-radio :label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系列:" prop="seriesCode">
              <input-with-dialog
                v-model="series"
                :props="{label:'seriesName',value:'seriesCode'}"
                :config="dialogConfigSeries"
                @change="handleChangeSeries"
              />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主选产品:" prop="mainProductName">
              <el-select
                v-model="formData.mainProductName"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in productList"
                  :key="item.dictCode"
                  :label="item.dictName"
                  :value="item.dictName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可选颜色:" prop="color">
              <el-input v-model="formData.color" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否默认勾选:" prop="isCheck">
              <el-radio-group v-model="formData.isCheck">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否必上:" prop="isUp">
              <el-radio-group v-model="formData.isUp">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDictByPatchType } from '@/api/dataDict'
import { saveData } from '@/api/seriesStandardColor'
export default {
  name: 'SeriesStandardColorEdit',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      productList: [],
      series: [{ value: '', label: '' }],
      dialogConfigSeries: {
        title: '系列',
        filtersRaw: [{ 'id': 'enable', 'property': 'enable', 'value': '1', 'operator': '=' }, { 'id': 'isPartakeSpread', 'property': 'isPartakeSpread', 'value': '1', 'operator': '=' }],
        interfacePath: `/slm-admin/api/v1/configSeries/page`,
        setting: [{ prop: 'seriesCode', title: '编码' }, { prop: 'seriesName', title: '名称' }]
      },
      formData: {
        seriesCode: '',
        seriesName: '',
        color: '', // 可选颜色
        cabinetColorCode: '',
        cabinetColorName: '',
        doorColorCode: '',
        doorColorName: '',
        mainProductName: '',
        isCheck: undefined,
        isUp: undefined,
        enable: 1
      },
      formRules: {
        color: [{ required: true, message: '请填写', trigger: 'change' }],
        mainProductName: [{ required: true, message: '请填写', trigger: 'change' }],
        seriesCode: [{ required: true, message: '请填写', trigger: 'change' }],
        isUp: [{ required: true, message: '请选择', trigger: 'change' }],
        isCheck: [{ required: true, message: '请选择', trigger: 'change' }]
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {},
  async  created() {
    if (this.detailData && this.detailData.id) {
      const { seriesCode,
        seriesName,
        color, // 可选颜色
        id,
        cabinetColorCode,
        cabinetColorName,
        doorColorCode,
        doorColorName,
        mainProductName,
        isCheck = undefined,
        isUp = undefined,
        enable = 1 } = this.detailData
      this.formData = { seriesCode,
        seriesName,
        color, // 可选颜色
        id,
        cabinetColorCode,
        cabinetColorName,
        doorColorCode,
        doorColorName,
        mainProductName,
        isCheck,
        isUp,
        enable }
      this.$set(this.formData, 'color', this.detailData.cabinetColorName || this.detailData.doorColorName)
      this.series = [{ value: this.detailData.seriesCode, label: this.detailData.seriesName }]
    }
    const { MAIN_PRODUCTS } = await getDictByPatchType(['MAIN_PRODUCTS'])
    this.productList = MAIN_PRODUCTS
  },
  methods: {
    handleChangeSeries(data) {
      if (data.length > 0) {
        this.$set(this.formData, 'seriesCode', data[0].seriesCode)
        this.$set(this.formData, 'seriesName', data[0].seriesName)
      } else {
        this.$set(this.formData, 'seriesCode', '')
        this.$set(this.formData, 'seriesName', '')
      }
    },
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {}
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          if (this.formData.mainProductName === '柜体') this.$set(this.formData, 'cabinetColorName', this.formData.color)
          if (this.formData.mainProductName === '门板') this.$set(this.formData, 'doorColorName', this.formData.color)
          saveData(this.formData).then(res => {
            if (res.success) {
              this.$message.success('保存成功')
              this.$emit('success', this.formData.id)
              this.closeDialog()
            }
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
